# FILE: FullTextSearch.py
# DESC: geonames fulltext search ws broker
#

# TODO: move to oreus.conf, context geonames
FULLTEXT_SEARCH_URL="http://ws.geonames.org/searchJSON?q=%s&country=%s&maxRows=%d&style=%s"
MAX_ROWS = 100

from web.service.WebServiceBroker import WebServiceBroker
from infra.logging import logger

class FullTextSearchBroker(WebServiceBroker):


	def isFromCountry(self, location, country):
		log = logger.getLogger()
		rv = False

		response = self.requestJSON(FULLTEXT_SEARCH_URL % (location, country.getCode(), 1, "MEDIUM"))
		try:
			rv = response["totalResultsCount"] > 0
		except KeyError:
			log.error("Could not find key \"totalresults\" in response = %s." % response)

		return rv 


	def getGeoNames(self, location):
		log = logger.getLogger()
		response = None
		geonames = None

		try:
			response = self.requestJSON(FULLTEXT_SEARCH_URL % (location, "", MAX_ROWS, "FULL"))
			geonames = response["geonames"]
		except KeyError:
			log.error("Could not find key \"geonames\" in response = %s." % response)

		return geonames 
